﻿package
{
	import fl.transitions.Wipe;
	
	import flash.display.MovieClip;
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.text.engine.RenderingMode;
	import flash.ui.Keyboard;
	
	import src.KeyboardListener;
	import src.Screen_Lateral;
	import src.User;
	
	public class Treppy extends Sprite
	{
		private var fileName:String = "Taller201401.xml";
		private var fileStr:FileStream = new FileStream();
		
		
		
		private var myXML:XML;		
		private var users:Array = new Array();
		
		private var display:Screen_Lateral;
		public var border:Sprite = new Sprite();
		private var maxHorizontal:Number;
		private var keyboardListener:KeyboardListener;
		
		public function Treppy(){
			maxHorizontal = stage.stageHeight*(Screen.mainScreen.visibleBounds.width/Screen.mainScreen.visibleBounds.height-stage.stageWidth/stage.stageHeight)
			
			keyboardListener = new KeyboardListener(this);
			updateXML('read');
//			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.nativeWindow.width=Screen.mainScreen.visibleBounds.width;
			stage.nativeWindow.height=Screen.mainScreen.visibleBounds.height;
			
			stage.align = StageAlign.RIGHT;
			
		}
		
//		Los Listeners son agregados en createDisplay()
		private function addListeners():void{
			display.title.addEventListener(MouseEvent.MOUSE_DOWN, startScrolling2);
			stage.addEventListener(MouseEvent.MOUSE_UP,stopScrolling2);			
		}
		
		public function startScrolling2(e:MouseEvent):void{
			trace(border.height, ' ', border.width);
			var bounds:Rectangle = new Rectangle(0,0,-maxHorizontal,0);
			border.startDrag(false,bounds);
			
		}		
		public function stopScrolling2(e:MouseEvent):void{
//			trace('border: ',border.x);
//			trace('mouseX: ',mouseX);
//			trace('mouseX: ',mouseY);
			border.stopDrag();
		}
		
		
		private function createDisplay():void{
			display = new Screen_Lateral(users,this);
			
			border.graphics.beginFill(0x666666,1);
//			border.graphics.lineStyle(0,0);
			border.graphics.drawRect(0,0,245,720);
			border.graphics.endFill();
			border.addChild(display);
			
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0x666666,1);
			mask.graphics.drawRect(0,0,240,705);
			mask.graphics.endFill();
			
			border.addChild(mask);		border.addChild(display);
			mask.x = 7.5;				display.x = 7.5;
			mask.y = 7.5;				display.y = 7.5;
			display.mask = mask;
			
			addChild(border);
			addListeners();
		}
		
		public function updateXML(mode:String):void{
			var fileURL:File = File.desktopDirectory.resolvePath(fileName);
			switch(mode){
				case 'read': 
					fileStr.openAsync(fileURL,FileMode.READ); 
					fileStr.addEventListener(Event.COMPLETE,read);
					break;
				case 'write': 
//					actualizar el XML Obj con los datos del Array
					for (var i:uint=0;i < myXML.user.length() ;i++){
						myXML.user[i].@username = users[i].username;
						myXML.user[i].@avatar = users[i].avatar ;
						myXML.user[i].@level = users[i].level;
						myXML.user[i].@points = users[i].points;
					}	
					
					fileStr.openAsync(fileURL,FileMode.WRITE);
					fileStr.writeUTFBytes(myXML.toXMLString());
					fileStr.close();
					break;
			}
			
		}
		
		private function read(e:Event):void{
			fileStr.removeEventListener(Event.COMPLETE,read);
			myXML = XML(fileStr.readUTFBytes(fileStr.bytesAvailable));
			fileStr.close();
			
			
			createUsers();
			//			empezar a trabajar con los datos en XML Obj
			createDisplay();
		}
		
		private function createUsers():void{
			for (var i:uint=0;i < myXML.user.length() ;i++){
				var user:User = new User();
				user.username = myXML.user[i].@username;
				user.avatar = myXML.user[i].@avatar;
				user.level = myXML.user[i].@level;
				user.points = myXML.user[i].@points;
				users[i] = user;
			}
		}
		
		
	}
}